<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SecondHandHouse;
use App\library\services\AgentService;
use App\library\services\FangAttrsService;
use App\Models\Agent;
use App\Models\Cities;
use App\Models\FangAttrs;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tab;

class SecondHandHouseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
      $tab = Tab::make();
      $tab->add('出租',$this->cloumnRent(),true);
      $tab->add('买卖',$this->cloumnSale());
      return  $tab;
    }

    protected function cloumnRent()
    {
        return Grid::make(new SecondHandHouse(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('agent_id', '经纪人姓名')->display(function ($res){
                if (empty($res)) {
                    return '';
                }
                return Agent::find($res)->nick_name;
            });
            $grid->column('house_type')->display(function ($res){
                if (empty($res)) {
                    return '';
                }
                return FangAttrs::find($res)->name;
            });
            $grid->column('price');
            $grid->column('image')->image();
            $grid->column('forward')->display(function ($res){
                if (empty($res)) {
                    return '';
                }
                return FangAttrs::find($res)->name;
            });
            $grid->column('area')->display(function ($res){
                if (empty($res)) {
                    return '';
                }
                return $res."平方米";
            });
            $grid->column('floor');
            $grid->column('address');
            $grid->column('province')->display(function ($res){
                if (empty($res)) {
                    return '';
                }
                return Cities::find($res)->name;
            });
            $grid->column('city')->display(function ($res){
                if (empty($res)) {
                    return '';
                }
                return Cities::find($res)->name;
            });
            $grid->column('region')->display(function ($res){
                if (empty($res)) {
                    return '';
                }
                return Cities::find($res)->name;
            });
            $grid->column('fit_up')->display(function ($res){
                if (empty($res)) {
                    return '';
                }
                return FangAttrs::find($res)->name;
            });
            $grid->column('house_estate');
            $grid->column('sale_type', '售卖方式')->display(function ($res){
                if ($res == 1){
                    return '出租';
                }else{
                    return  '买卖';
                }
            });
            $grid->column('rent_type', '租赁方式')->display(function ($res){
                if (empty($res)) {
                    return '';
                }
                return FangAttrs::find($res)->name;
            });
            $grid->column('property');
            $grid->column('connect_name');
            $grid->column('days');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    protected function cloumnSale()
    {
        return Grid::make(\App\Models\SecondHandHouse::where('sale_type',2), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('agent_id', '经纪人姓名')->display(function ($res){
                return Agent::find($res)->nick_name;
            });
            $grid->column('house_type')->display(function ($res){
                return FangAttrs::find($res)->name;
            });
            $grid->column('price');
            $grid->column('unit_price');
            $grid->column('image')->image();
            $grid->column('forward')->display(function ($res){
                return FangAttrs::find($res)->name;
            });
            $grid->column('area')->display(function ($res){
                return $res."平方米";
            });
            $grid->column('floor');
            $grid->column('address');
            $grid->column('province')->display(function ($res){
                return Cities::find($res)->name;
            });
            $grid->column('city')->display(function ($res){
                return Cities::find($res)->name;
            });
            $grid->column('region')->display(function ($res){
                return Cities::find($res)->name;
            });
            $grid->column('fit_up')->display(function ($res){
                return FangAttrs::find($res)->name;
            });
            $grid->column('house_estate');
            $grid->column('sale_type', '售卖方式')->display(function ($res){
                if ($res == 1){
                    return '出租';
                }else{
                    return  '买卖';
                }
            });
            $grid->column('property');
            $grid->column('connect_name');
            $grid->column('days');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SecondHandHouse(), function (Show $show) {
            $show->field('id');
            $show->field('agent_id');
            $show->field('house_type');
            $show->field('unit_price');
            $show->field('price');
            $show->field('image');
            $show->field('images');
            $show->field('forward');
            $show->field('area');
            $show->field('floor');
            $show->field('address');
            $show->field('province');
            $show->field('city');
            $show->field('region');
            $show->field('fit_up');
            $show->field('house_estate');
            $show->field('config');
            $show->field('sale_type');
            $show->field('rent_type');
            $show->field('property');
            $show->field('connect_name');
            $show->field('tags');
            $show->field('days');
            $show->field('latitude');
            $show->field('longitude');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        //经纪下拉
        $agents = AgentService::getAgents();
        $agent = [];
        foreach ($agents as $value) {
            $agent[$value['id']] = $value['nick_name'];
        }
        //户型下拉
        $houseTypes = FangAttrsService::getHouseType();
        $houseType = [];
        foreach ($houseTypes as $value) {
            $houseType[$value['id']] = $value['name'];
        }
        //朝向下拉
        $houseDirections = FangAttrsService::getFangDirection();
        $houseDirection = [];
        foreach ($houseDirections as $value) {
            $houseDirection[$value['id']] = $value['name'];
        }
        //装修程度下拉
        $fangFits = FangAttrsService::getFangFit();
        $fangFit = [];
        foreach ($fangFits as $value) {
            $fangFit[$value['id']] = $value['name'];
        }
        //租赁方式
        $fangRents = FangAttrsService::getFangRent();
        $fangRent = [];
        foreach ($fangRents as $value) {
            $fangRent[$value['id']] = $value['name'];
        }
        //配套设施
        $fangConfigs = FangAttrsService::getFangConfig();
        $fangConfig = [];
        foreach ($fangConfigs as $value) {
            $fangConfig[$value['id']] = $value['name'];
        }
        return Form::make(new SecondHandHouse(),
            function (Form $form) use ($agent, $houseType, $houseDirection, $fangFit, $fangRent, $fangConfig) {
                $form->display('id');
                $form->select('agent_id', '经纪人名称')->options($agent);
                $form->select('house_type')->options($houseType);
                $form->multipleImage('images');
                $form->select('fit_up')->options($fangFit);
                $form->select('forward')->options($houseDirection);
                $form->text('area');
                $form->text('floor');
                $form->text('address');
                $form->select('province')->options(
                    Cities::where('pid', 0)->get()->pluck('name', 'id')
                )->load('city', '/city');
                // 城市
                $form->select('city')->load('region', '/city');
                // 区域
                $form->select('region');
                $form->text('house_estate');
                $form->text('property');
                $form->text('connect_name');
                $form->text('tags');
                $form->text('days');
                $form->editor('desc', '房源描述');
                $form->radio('sale_type', '售卖方式')
                    ->when(1, function (Form $form) use ($fangRent, $fangConfig) {
                        $form->select('rent_type', '租赁方式')->options($fangRent);
                        $form->checkbox('config')
                            ->options($fangConfig)
                            ->saving(function ($value) {
                                return json_encode($value);
                            });
                        $form->text('price','月租');
                    })->when(2, function (Form $form) {
                        $form->text('unit_price');
                        $form->text('price');
                    })->options([
                        1 => '出租',
                        2 => '买卖'
                    ]);
                $form->display('created_at');
                $form->display('updated_at');
            });
    }
}
